package com.shenhaofeng.app.widget.banner;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.View;

import com.shenhaofeng.app.R;


/**
 * Banner的指示器，该view已经包含在Banner中
 * Created by Administrator on 2016/1/20.
 */
public class BannerIndicator extends View implements Banner.onPageListener {

    //中心点的间距
    private int indicatorSpace = 60;
    //指示器的数量
    private int indicatorCount = 0;
    private int selectedPosition = -1;

    private int indicatorSize = 30;

    private Paint paint;
    private int selectedColor;
    private int standardColor;
    private IndicatorDrawer indicatorDrawer;


    public BannerIndicator(Context context) {
        super(context);
        init();
    }


    public BannerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public BannerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getSuggestedMinimumWidth(), getSuggestedMinimumHeight());
    }


    @Override
    protected void onDraw(Canvas canvas) {
        int indicatorWidth = getIndicatorWidth();
        int indicatorHeight = getIndicatorHeight();
//        int radio = Math.min(indicatorHeight, indicatorWidth) / 2;
//        int y = getHeight() / 2;
        for (int i = 0; i < indicatorCount; i++) {
            int x = i * indicatorSpace + indicatorWidth / 2 + getPaddingLeft();
            int left = x - indicatorWidth / 2;
            int top = 0;
            int right = x + indicatorWidth / 2;
            int bottom = getHeight();
            if (i == selectedPosition) {
                paint.setColor(selectedColor);
                getIndicatorDrawer().onActiveDraw(canvas, left, top, right, bottom, paint);
            } else {
                paint.setColor(standardColor);
                getIndicatorDrawer().onStandardDraw(canvas, left, top, right, bottom, paint);
            }
        }
    }


    @Override
    protected int getSuggestedMinimumWidth() {
        //指示器的间隔数
        int spaceCount = indicatorCount - 1;
        spaceCount = spaceCount >= 0 ? spaceCount : 0;
        int minSize = getIndicatorWidth()
                + spaceCount * indicatorSpace
                + getPaddingLeft() + getPaddingRight();
        return Math.max(super.getSuggestedMinimumWidth(), minSize);
    }


    @Override
    protected int getSuggestedMinimumHeight() {
        int minSize = getIndicatorHeight() + getPaddingTop() + getPaddingRight();
        return Math.max(super.getSuggestedMinimumHeight(), minSize);
    }


    private void init() {
        TypedArray ta = getContext().getTheme().obtainStyledAttributes(new int[]{R.attr.colorPrimary});
        selectedColor = ta.getColor(0, Color.RED);
        standardColor = Color.GRAY;
        ta.recycle();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
    }


    public void setIndicatorDrawer(IndicatorDrawer indicatorDrawer) {
        this.indicatorDrawer = indicatorDrawer;
    }


    private IndicatorDrawer getIndicatorDrawer() {
        if (indicatorDrawer == null) {
            indicatorDrawer = new DefaultIndicatorDrawer();
        }
        return indicatorDrawer;
    }


    protected int getIndicatorHeight() {
        return indicatorSize;
    }


    protected int getIndicatorWidth() {
        return indicatorSize;
    }


    /**
     * 设置指示器的数量
     *
     * @param count
     */
    public void setIndicatorCount(int count) {
        this.indicatorCount = count;
        requestLayout();
    }


    /**
     * 设置选中的指示器
     *
     * @param position
     */
    public void selectIndicator(int position) {
        this.selectedPosition = position;
        postInvalidate();
    }


    protected void syncBanner(Banner banner) {
        setIndicatorCount(banner.getCount());
        selectIndicator(banner.getCurrentItem());
    }


    public void setSelectedColor(@ColorInt int color) {
        this.selectedColor = color;
        postInvalidate();
    }


    public void setStandardColor(@ColorInt int color) {
        this.standardColor = color;
        postInvalidate();
    }


    public void setIndicatorSize(int size) {
        this.indicatorSize = size;
        postInvalidate();
    }


    public void setIndicatorSpace(int space) {
        this.indicatorSpace = space;
        postInvalidate();
    }


    @Override
    public void onPageSelected(int position) {

    }


    @Override
    public void onPageCountChange(Banner banner) {
        syncBanner(banner);
    }




    public class DefaultIndicatorDrawer implements IndicatorDrawer {

        @Override
        public void onStandardDraw(Canvas canvas, int left, int top, int right, int bottom, Paint paint) {
            float x = (right + left) / 2;
            float y = (top + bottom) / 2;
            canvas.drawCircle(x, y, (float) (Math.min(x, y) * 4.0 / 5.0), paint);
        }


        @Override
        public void onActiveDraw(Canvas canvas, int left, int top, int right, int bottom, Paint paint) {
            float x = (right + left) / 2;
            float y = (top + bottom) / 2;
            canvas.drawCircle(x, y, Math.min(x, y), paint);
        }
    }
}
